package com.hz.ys_stock.dao;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hz.framework.MyAction.BaseAction;
import com.hz.framework.MyInterFace.SystemInterFace;
import com.hz.framework.dao.CommonDBDao;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.dao.DBQ;
import com.hz.framework.util.CommonFunc;
import com.hz.framework.util.DateUtil;
import com.hz.framework.util.SqlParameter;
import com.hz.ys_stock.bean.PresMakings;
import com.hz.ys_stock.bean.PresPurchase;
import com.hz.ys_stock.bean.PresStockspecial;
import com.hz.ys_stock.dbsql.StockSql;
import com.hz.ys_system.dao.QueryDao;

/**
 * *****************************************************************************
 *               Copyright(c) 2010 by 哈尔滨华泽数码科技有限公司.
 *                       All rights reserved.
 *******************************************************************************
 *     File Name         :  com.hz.ys_stock.dao.PurchaseDao.java
 *     Description(说明)	:  供应商管理
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description	  
 *     1		Jun 8, 2011				韩飞	              Created
 *****************************************************************************
 */
public class StockDao implements SystemInterFace{
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能: 查询库存 信息
	 */
	public String SelStock(HttpServletRequest request,String flag){
		StringBuilder sbSQL = new StringBuilder();
		sbSQL.append(StockSql.SEL_STOCK_INFO);
		sbSQL.append(" and m.makingtype = '"+flag+"' ");
		
		sbSQL.append(new QueryDao().QueryInfo(request , new SqlParameter("goodsname","standard",
				"goodstype","brand") , "like") );
		
		return sbSQL.toString();
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能: 查询历史记录库存 信息
	 */
	public String SelStock_log(HttpServletRequest request,String flag){
		StringBuilder sbSQL = new StringBuilder();
		sbSQL.append(StockSql.SEL_STOCK_LOG);
		sbSQL.append(" and m.makingtype = '"+flag+"' ");
		
		sbSQL.append(new QueryDao().QueryInfo(request , new SqlParameter("goodsname","standard",
				"goodstype","brand") , "like") );
		
		return sbSQL.toString();
	}
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param request
	 * @return 
	 * 方法功能:
	 */
	public boolean Del_Stock(HttpServletRequest request){
		String sid = CommonDao.isnull(request.getParameter("sid"));
		CommonDBDao dbo= new CommonDBDao();
		if(dbo.executeSQL(StockSql.INS_STOCK_LOG, new SqlParameter(sid))){
			return dbo.executeSQL(StockSql.DEL_STOCK_INFO, new SqlParameter(sid));
		}else{
			return false;
		}
	}
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param goodsid  物料编号 
	 * @return 
	 * 方法功能:  根据物料编号查询库存的物料信息 ， 或者查询所有库存物料信息
	 */
	public List SelStock_allInfo(String goodsid){
		
		CommonDBDao dbo= new CommonDBDao();
		if("".equals(goodsid)){
//			查询所有库存物料信息
			return dbo.DBjsonList(StockSql.SEL_STOCK_INFO);
		}else{
//			根据物料编号查询库存的物料信息
			return dbo.queryListByPS(StockSql.SEL_STOCK_GOODSID, new SqlParameter(goodsid),null);
		}
		
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param goodsid  物料编号 
	 * @return 
	 * 方法功能:  根据物料编号查询库存的物料信息 ， 或者查询所有库存物料信息
	 */
	public boolean Stock_Label(List goodslist){
		boolean ins = false;
		DBQ db=new DBQ();
		try {
			if(goodslist!=null){
				String str = "";
				for(int i = 0;i<goodslist.size();i++){
					Map map = (Map)goodslist.get(i);
					ResultSet rs = db.getResults("select goodsid from pres_stock where goodsid = '"+map.get("goodsid")+"'");
					//有库存数据 进行更新
					if(rs.next()){
						str ="update pres_stock set numnow=numnow+"+map.get("sumgoods")+",numreal=numreal+"+map.get("sumgoods")+",numbuy=numbuy+"+map.get("sumgoods")+" where goodsid = '"+map.get("goodsid")+"'";
					}else{
//					没有 进行插入数据
						str = "insert into pres_stock (goodsid,numnow,numreal,numbuy,numuse) values" +
						"('"+map.get("goodsid")+"','"+map.get("sumgoods")+"','"+map.get("sumgoods")+"','"+map.get("sumgoods")+"','0')";
					}
					System.out.println(str);
					ins = db.execute(str);
				}
			}
		} catch (SQLException e) {
			System.out.println(" Stock_Label 方法异常");
			e.printStackTrace();
		}finally{
			db.close();
		}
		return ins;
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能: 查询库存 信息
	 */
	public List SelStockList(HttpServletRequest request,String flag){
		StringBuilder sbSQL = new StringBuilder();
		sbSQL.append(StockSql.SEL_STOCK_INFO);
		sbSQL.append(" and m.makingtype = '"+flag+"'");
		CommonDBDao dbo= new CommonDBDao();
		
		return dbo.DBjsonList(sbSQL.toString());
	}
	/**
	 * @author 韩飞
	 * @time Dec 24, 2011	
	 * @param mak
	 * @return
	 * @throws Exception 
	 * 方法功能: 保存特殊入库信息
	 */
	public boolean DoSaveSpecial( PresStockspecial ps ) throws Exception {
		boolean ins = false;
		CommonDao dbd= new CommonDao();
		if(ps!=null){
			//保存或更新 下拉选名称表
			ps.setCreatetime(DateUtil.getDateTime());
			if("出库".equals(ps.getCrflag())){
				ps.setRightnum(0-ps.getRightnum());
			}
			ins = dbd.insertinfor(ps);
			if(ins){
				String sql ="update pres_stock set numnow=numnow+"+ps.getRightnum()+",numreal=numreal+"+ps.getRightnum()+" where goodsid = '"+ps.getGoodsid()+"'";
				dbd.insorupd(sql);
			}
		}
		return ins;
	}
	//删除特殊入库信息
	public boolean Del_Special(HttpServletRequest request){
		boolean ins = false;
		String sid = CommonDao.isnull(request.getParameter("sid"));
		CommonDao dbo= new CommonDao();
		PresStockspecial ps = (PresStockspecial)dbo.hibernateGet("com.hz.ys_stock.bean.PresStockspecial",Integer.parseInt(sid));
		String[] sql ={"update pres_stock set numnow=numnow-"+ps.getRightnum()+",numreal=numreal-"+ps.getRightnum()+" where goodsid = '"+ps.getGoodsid()+"'",
				"delete from pres_stockspecial where id = "+sid};
		ins = dbo.insorupdArray(sql);
		return ins;
	}
	/**
	 * 
	 * @author 韩飞
	 * @time Dec 24, 2011	
	 * @param request
	 * @param flag
	 * @return 
	 * 方法功能:
	 */
	public String SelSpecial(HttpServletRequest request,String flag){
		StringBuilder sbSQL = new StringBuilder();
		sbSQL.append(StockSql.SEL_SPECIAL_INFO);
		sbSQL.append(" and makingtype = '"+flag+"'");
		
		System.out.println(sbSQL.toString());
		return sbSQL.toString();
	}
}
